/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package com.aionemu.gameserver.model.gameobjects.instance;

import com.aionemu.gameserver.dataholders.DataManager;
import com.aionemu.gameserver.model.EmotionType;
import com.aionemu.gameserver.model.gameobjects.Creature;
import com.aionemu.gameserver.model.gameobjects.NpcWhichNotGatherable;
import com.aionemu.gameserver.model.gameobjects.VisibleObject;
import com.aionemu.gameserver.model.gameobjects.interfaces.IDialogRequest;
import com.aionemu.gameserver.model.gameobjects.interfaces.IDialogSelect;
import com.aionemu.gameserver.model.gameobjects.interfaces.IReward;
import com.aionemu.gameserver.model.gameobjects.knownList.StaticObjectKnownList;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.model.gameobjects.state.CreatureState;
import com.aionemu.gameserver.model.gameobjects.stats.StaticNpcGameStat;
import com.aionemu.gameserver.model.templates.npcTemplate.NpcTemplate;
import com.aionemu.gameserver.model.templates.spawn.SpawnTemplate;
import com.aionemu.gameserver.network.aion.serverpackets.SM_DIALOG_WINDOW;
import com.aionemu.gameserver.network.aion.serverpackets.SM_EMOTION;
import com.aionemu.gameserver.network.aion.serverpackets.SM_NPC_INFO;
import com.aionemu.gameserver.questEngine.QuestEngine;
import com.aionemu.gameserver.questEngine.model.QuestEnv;
import com.aionemu.gameserver.services.DialogService;
import com.aionemu.gameserver.taskmanager.tasks.DecayTaskManager;
import com.aionemu.gameserver.utils.PacketSendUtility;

/**
 * @author Mr. Poke, Lyahim
 *
 */
public class StaticNpc extends NpcWhichNotGatherable implements IDialogRequest, IDialogSelect
{
	private boolean	isDie	= false;

	/**
	 * @param objId
	 * @param spawnTemplate
	 * @param position
	 */
	public StaticNpc(int objId, SpawnTemplate spawnTemplate)
	{
		super(objId, spawnTemplate);
		if (spawnTemplate != null)
			this.objectTemplate = DataManager.NPC_DATA.getNpcTemplate(spawnTemplate.getTemplateId());
		this.setKnownlist(new StaticObjectKnownList(this));
		this.setGameStats(new StaticNpcGameStat(this));

	}

	@Override
	public NpcTemplate getObjectTemplate()
	{
		return (NpcTemplate) super.getObjectTemplate();
	}

	public void see(VisibleObject object)
	{
		if (object instanceof Player)
			PacketSendUtility.sendPacket((Player) object, new SM_NPC_INFO(this, (Player) object));
	}

	public void onDie(Creature lastAttacker)
	{
		synchronized (this)
		{
			if (isDie)
				return;
			isDie = true;
		}
		DecayTaskManager.getInstance().addDecayTask(this);

		scheduleRespawn();
		if (lastAttacker != null)
		{
			setTarget(lastAttacker);
			if (this instanceof IReward)
				((IReward) this).doReward();
		}

		setState(CreatureState.DEAD);
		unsetState(CreatureState.COMBAT);
		setTarget(null);
		PacketSendUtility.broadcastPacketAndReceive(this, new SM_EMOTION(this, EmotionType.DIE, 0, lastAttacker == null ? 0 : lastAttacker.getObjectId()));
	}
	
	/* (non-Javadoc)
	 * @see com.aionemu.gameserver.model.gameobjects.interfaces.IDialogSelect#onDialogSelect(int, com.aionemu.gameserver.model.gameobjects.player.Player, int)
	 */
	@Override
	public boolean onDialogSelect(int dialogId, Player player, int questId)
	{
		DialogService.onDialogSelect(dialogId, player, this, questId);
		return true;
	}
	
	@Override
	public void onDialogRequest(final Player player)
	{
		if (QuestEngine.getInstance().onDialog(new QuestEnv(this, player, 0, -1)))
			return;
		// TODO need check here
		PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(getObjectId(), 10));
	}
}
